﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameServer.Networking.Packets;
namespace GameServer.Networking.Handlers
{
    class HANDLE_COUPON_BUY : PacketHandler
    {
        public override void Handle(Virtual_Objects.User.virtualUser User)
        {
            try
            {
                string WeaponToBuy = getNextBlock();
                int Days = 0;
                int CouponToRemove = 0;
                #region Calculate
                switch (WeaponToBuy)
                {
                    case "DB12": Days = 3; CouponToRemove = 5; break;
                    case "CJ01": Days = 3; CouponToRemove = 9; break;
                    case "DB13": Days = 3; CouponToRemove = 15; break;
                    case "DF43": Days = 7; CouponToRemove = 20; break;
                    case "CK01": Days = 7; CouponToRemove = 25; break;
                    case "DC31": Days = 10; CouponToRemove = 30; break;
                    case "DG21": Days = 10; CouponToRemove = 35; break;
                    case "CI01": Days = 10; CouponToRemove = 40; break;
                    case "DC65": Days = 30; CouponToRemove = 250; break;
                    default: User.disconnect(); break;
                }
                #endregion
                if (Program.ESL == false)
                {
                    if (User.Coupons >= CouponToRemove)
                    {
                        int InventorySlot = -1;
                        for (int I = 0; I < 30; I++)
                        {
                            if (User.Inventory[I] == null) { InventorySlot = I; break; }

                        }
                        if (InventorySlot >= 0)
                        {
                            User.Coupons -= CouponToRemove;
                            DB.runQuery("UPDATE users SET coupons='" + User.Coupons + "' WHERE id='" + User.UserID + "'");
                            User.AddItem(WeaponToBuy, Days);
                            User.send(new PACKET_COUPON_BUY(WeaponToBuy, User));
                            User.send(new PACKET_COUPON_EVENT(User));
                            User.send(new PACKET_CREDITS(User));
                            User.send(new PACKET_CHARACTER_INFO(User));
                        }
                        else
                        {
                            User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                        }
                    }
                    else
                    {
                        User.send(new PACKET_COUPON_BUY(PACKET_COUPON_BUY.ErrorCode.NotEnoughCoupons));
                    }
                }
                else
                    User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, "NULL"));
            }
            catch { }
        }
    }
}